package gupao.concurrency.api.sample;

/**
 * 每个API调用的基本信息
 * 
 * @author wilson
 */
public final class ApiInvokeInfo
{

	/**
	 * 调用的API名字
	 */
	public final String apiName;
	/**
	 * 调用API的耗时，时间单位毫秒
	 */
	public final long times;
	/**
	 * 调用API的信息内容
	 */
	public final String msg;

	/**
	 * 调用API发生的异常,null表示调用成功，否则表示调用失败有异常
	 */
	public final transient Exception exp;

	private ApiInvokeInfo(String apiName, long times, Exception exp)
	{
		this.apiName = apiName;
		this.times = times;
		this.exp = exp;

		if (exp == null) {
			msg = "success";
		}
		else {
			msg = exp.getMessage();
		}
	}

	public static ApiInvokeInfo createErrorInfo(String apiName, long times, Exception errorMsg)
	{
		return new ApiInvokeInfo(apiName, times, errorMsg);
	}

	public static ApiInvokeInfo createSuccessInfo(String apiName, long times)
	{
		return new ApiInvokeInfo(apiName, times, null);
	}

	@Override
	public String toString() {
		return "ApiInvokeInfo{" +
				"apiName='" + apiName + '\'' +
				", times=" + times +
				", msg='" + msg + '\'' +
				", exp=" + exp +
				'}';
	}
}
